package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.ApArticleConfigMapper;
import com.zhentao.mapper.ApArticleContentMapper;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.dtos.ArticleDto;
import com.zhentao.model.article.dtos.ArticleHomeDto;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.article.pojo.ApArticleConfig;
import com.zhentao.model.article.pojo.ApArticleContent;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.enums.AppHttpCodeEnum;
import com.zhentao.service.ApArticleContentService;
import com.zhentao.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.nntp.Article;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentService apArticleContentService;

    //设置size常量
    private final static  short Max_PAGE_SIZE = 50;

    /**
     * 客户端 首页文章
     * @param i  1加载更多  2 加载最新的
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Integer i, ArticleHomeDto dto) {
        Integer size = dto.getSize();

        //设置size默认值为10
        if(size == null || size == 0){
            size = 10;
        }

        //如果size<50  size = size  如果size>50 size=50
        size = Math.min(size, Max_PAGE_SIZE);
        //将size赋值给dto
        dto.setSize(size);

        //校验参数 频道
        if(StringUtils.isEmpty(dto.getTag())){
            dto.setTag("__all__");
        }

        //校验参数 时间
        if(dto.getMaxBehotTime() == null){
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime() == null){
            dto.setMinBehotTime(new Date());
        }

        //查询数据
        List<ApArticle> apArticleList = articleMapper.load(dto,i);

        //将结果响应给前端
        return ResponseResult.okResult(apArticleList);
    }

    /**
     * 文章同步  自媒体发布的文章同步到手机端
     * @param dto  dto
     * @return  ResponseResult
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //参数校验
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //参数封装
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        //进行保存  判断新增还是修改
        if(dto.getId() == null){//没有携带id 就是添加
            //保存文章基本信息
            articleMapper.insert(apArticle);
            //保存内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);

            //保存配置表
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setArticleId(apArticle.getId());
            apArticleConfig.setIsComment(true);
            apArticleConfig.setIsDelete(false);
            apArticleConfig.setIsDown(false);
            apArticleConfig.setIsForward(true);
            apArticleConfigMapper.insert(apArticleConfig);
        }else {
            //没有存在id 修改文章
            articleMapper.updateById(apArticle);

            //修改内容
            QueryWrapper<ApArticleContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleContent::getArticleId, dto.getId());
            ApArticleContent content = apArticleContentMapper.selectOne(queryWrapper);
            content.setContent(dto.getContent());
            apArticleContentMapper.updateById(content);

            //修改配置表
//            ApArticleConfig apArticleConfig = new ApArticleConfig();
//            apArticleConfig.setArticleId(apArticle.getId());
//            apArticleConfig.setIsComment(true);
//            apArticleConfig.setIsDelete(false);
//            apArticleConfig.setIsDown(false);
//            apArticleConfig.setIsForward(true);
//            apArticleConfigMapper.updateById(apArticleConfig);
        }
        //生成url 修改数据库
        apArticleContentService.createStaticUrl(apArticle.getId());


        return  ResponseResult.okResult(apArticle.getId());
    }
}
